package com.hoodbrains.insideloc.shopping;

import java.util.ArrayList;

import com.hoodbrains.insideloc.R;
import com.hoodbrains.insideloc.utils.Catalog;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class RayonChoiceActivity extends ListActivity {
	Context mContext;
	
	private boolean editing = false;
	
	private class RayonAdapter extends ArrayAdapter<String>{
		private LayoutInflater inflater;
		
		
		public RayonAdapter(RayonChoiceActivity rayonChoiceActivity,
				int rayonChoice, ArrayList<String> rayons) {
			super(rayonChoiceActivity, rayonChoice, rayons);
			inflater = LayoutInflater.from(rayonChoiceActivity);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.rayon_list_item, parent, false);
			}
			
			TextView name = (TextView) convertView.findViewById(R.id.rayon_sli);
			
			name.setText(Catalog.getInstance().getRayonsEdit().get(position));
			String color = Catalog.getInstance().getColor(Catalog.getInstance().getRayonsEdit().get(position));

			name.setTextColor(Color.parseColor(color));
			//name.setTextSize(19);
			
			return convertView;
			
		}
		
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		mContext = this;
		
		getListView().setBackgroundResource(R.drawable.background);
		getListView().setCacheColorHint(0x00000000);
		
		if (getIntent().hasExtra("rayon_position")) {
			editing = true;
		} else {
			editing = false;
		}
		
		ArrayList<String> res = editing ?Catalog.getInstance().getRayonsEdit() : Catalog.getInstance().getRayonsView();

		setListAdapter(new RayonAdapter(this, R.layout.rayon_choice, res));
		
		final ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		
		lv.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view,
		        int position, long id) {
		    	
		    	if (editing) {
		    		
		    		setResult(RESULT_OK, getIntent().putExtra("rayon", (String)lv.getAdapter().getItem(position)));
		    		finish();
		    	} else {
		      // When clicked, show a toast with the TextView text
		      Intent intent = new Intent(mContext, ProductChoiceActivity.class);
		      	intent.putExtra("type", (String)lv.getAdapter().getItem(position));
				startActivityForResult(intent, 18);
		    	}
		    }
		  });
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
    	setResult(RESULT_OK, data);
    	finish();
	}
}
